local skel = fk.CreateSkill {
  name = "lb__xuanji",
  --tags = { Skill.Compulsory },
}

Fk:loadTranslationTable {
  ["lb__xuanji"] = "旋击",
  --[":lb__xuanji"] = "<b>锁定技</b>，你发动过<a href=':lb__kanxi'><font color='red'>〖勘析〗</font></a>的回合结束时，你翻面X次（X为你因〖勘析〗增加的攻击范围）；当你翻至背面后，视为使用一张【杀】，每轮因此使用的【杀】指定过所有合法目标前，无法指定相同目标。",
  --9.23
  [":lb__xuanji"] = "你发动过<a href=':lb__kanxi'><font color='red'>〖勘析〗</font></a>的回合结束时，你可以翻面X次（X为你因〖勘析〗增加的攻击范围）；当你翻至背面后，视为使用一张【杀】，每轮因此使用的【杀】指定过所有合法目标前，无法指定相同目标。",

  ["#lb__xuanji"] = "旋击：你可以翻面 %arg 次",

  ["@[player]lb__xuanji-round"] = "旋击",

  ["$lb__xuanji1"] = "骨碌碌~",
  ["$lb__xuanji2"] = "转圈圈咯！",
}


skel:addEffect(fk.TurnEnd, {
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and player:usedSkillTimes("lb__kanxi", Player.HistoryTurn) > 0
        and player:getMark("@lb__kanxi-round") > 0
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player,
      { skill_name = skel.name, prompt = "#lb__xuanji:::" .. player:getMark("@lb__kanxi-round") })
  end,
  on_use = function(self, event, target, player, data)
    for i = 1, player:getMark("@lb__kanxi-round"), 1 do
      if not player.dead then
        player:turnOver()
        player.room:delay(850)
      end
    end
  end,
})

skel:addEffect(fk.TurnedOver, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and not player.faceup
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = Fk:cloneCard("slash"):getAvailableTargets(player)
    if #tos == 0 then return end
    local targets = table.filter(tos, function(to)
      return not table.contains(player:getTableMark("@[player]lb__xuanji-round"), to.id)
    end)
    if #targets == 0 then
      room:setPlayerMark(player, "@[player]lb__xuanji-round", 0)
      targets = tos
    end
    local use = player.room:askToUseVirtualCard(player, {
      name = "slash",
      cancelable = false,
      skill_name = skel.name,
      extra_data = { exclusive_targets = table.map(targets, Util.IdMapper) },
      skip = true,
    })
    if use then
      for _, to in ipairs(use.tos) do
        room:addTableMarkIfNeed(player, "@[player]lb__xuanji-round", to.id)
      end
      room:useCard(use)
    end
  end,
})

return skel
